package com.example.tellwin.home.adpater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.R;
import com.example.tellwin.home.bean.WorkBean;
import com.example.tellwin.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : LinZePeng
 * @date : 2020/11/11
 * @Describe ：
 **/
public class WorkAdapter extends RecyclerView.Adapter<WorkAdapter.WorkViewHolder> {

    private List<WorkBean> workBeans = new ArrayList<>();
    Context context;
    WorkListener workListener;
    boolean isEnd;
    int type;

    public void setWorkBeans(List<WorkBean> workBeans, boolean isEnd, int type) {
        this.workBeans = workBeans;
        this.isEnd = isEnd;
        this.type = type;
        notifyDataSetChanged();
    }

    public void setWorkListener(WorkListener workListener) {
        this.workListener = workListener;
    }

    @NonNull
    @Override
    public WorkViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new WorkViewHolder(LayoutInflater.from(context).inflate(R.layout.item_work, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull WorkViewHolder holder, int position) {
        WorkBean workBean = workBeans.get(position);
        holder.workIndexTv.setText((position + 1) + "");
        holder.workNameTv.setText(workBean.getRealName());
        holder.pollNumberTv.setText(workBean.getVoteCount() + "票");
        holder.voteTv.setOnClickListener(v -> {
            if (workListener != null) {
                workListener.onItemVote(position, workBean);
            }
        });

        if (isEnd) {
            holder.voteTv.setBackgroundResource(R.drawable.color_9c9c9c_corners_4_shape);
            holder.voteTv.setEnabled(false);
            holder.voteTv.setText(R.string.vote);
        } else {
            if (workBean.getIsVote() == 1) {
                holder.voteTv.setBackgroundResource(R.drawable.color_9c9c9c_corners_4_shape);
                holder.voteTv.setEnabled(false);
                holder.voteTv.setText("已投");
            } else {
                holder.voteTv.setBackgroundResource(R.drawable.green_corners_4_shape);
                holder.voteTv.setEnabled(true);
                holder.voteTv.setText(R.string.vote);
            }
        }

        holder.mediaPlayIv.setVisibility(View.GONE);
        if (type == 0) {
            GlideUtils.LoadImage(context, workBean.getWorks(), holder.workIv);
        } else if (type == 1) {
            holder.workIv.setBackground(context.getResources().getDrawable(R.drawable.black_60_corners_4_shape));
            holder.mediaPlayIv.setVisibility(View.VISIBLE);
        } else {
            holder.workIv.setBackground(context.getResources().getDrawable(R.drawable.black_60_corners_4_shape));
            holder.mediaPlayIv.setVisibility(View.VISIBLE);
        }
        holder.workIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (workListener != null) {
                    workListener.onItemClick(position, workBean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return workBeans.size();
    }

    class WorkViewHolder extends RecyclerView.ViewHolder {

        TextView workNameTv;
        TextView workIndexTv;
        ImageView workIv;
        TextView pollNumberTv;
        TextView voteTv;
        ImageView mediaPlayIv;

        public WorkViewHolder(@NonNull View itemView) {
            super(itemView);
            workNameTv = (TextView) itemView.findViewById(R.id.work_name_tv);
            workIndexTv = (TextView) itemView.findViewById(R.id.work_index_tv);
            workIv = (ImageView) itemView.findViewById(R.id.work_iv);
            pollNumberTv = (TextView) itemView.findViewById(R.id.poll_number_tv);
            voteTv = (TextView) itemView.findViewById(R.id.vote_tv);
            mediaPlayIv = itemView.findViewById(R.id.media_play_iv);
        }
    }

    public interface WorkListener {
        void onItemVote(int position, WorkBean workBean);

        void onItemClick(int position, WorkBean workBean);
    }
}
